package com.cms.web.queryAnalyse;

import java.util.Calendar;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.metadata.bizmodel.SYS_MODEL;
import com.mrp.persistence.queryAnalyse.pageQuery.bean.EMPPageQuery;
import com.persistence.qxgl.beans.SYSUser;
import com.web.BaseController;

import com.cms.biz.queryAnalyse.CMSQueryAnalyseServiceMgr;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.server.provider.FormModelDataProvider;
import com.enums.EMPQueryParamEnum;

import dwz.common.util.DateUtil;

@Controller
@RequestMapping(value="/cmsPSHTQueryAnalyse")
public class EMPCMSPSHTQueryAnalyseController extends BaseController{
	@Autowired
	private                CMSQueryAnalyseServiceMgr                       queryAnalyseServiceMgr;

	@Autowired
	private                    FormModelDataProvider                                formModelUtil;

	@Autowired
	public HttpSession session;

	/**
	 * 材料明细查询参数选择界面
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/preQueryForm/PSHTAnalyseQueryPage")
	public String materialQueryPage(@RequestParam String action, @RequestParam String navTabId, @RequestParam String title, @RequestParam String returnURL, Model model) {
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		
		model.addAttribute("F_QSRQ", DateUtil.date3String("yyyy") + "-01-01");
		model.addAttribute("F_JZRQ", DateUtil.date2String(DateUtil.getLastDayOfMonth(Calendar.getInstance()), "yyyy-MM-dd"));
		model.addAttribute("F_CODE", user.getUSER_ORGID());
		model.addAttribute("action", action);
		model.addAttribute("navTabId", navTabId);
		model.addAttribute("title", title);
		return returnURL;
	}
	
	/**
	 * 生产单剩余数量
	 * @param F_CKBH
	 * @param model
	 * @return
	 */
	@RequestMapping("/PSHTAnalyseQuery")
	public String PSHTCQBMQueryAnalyse(@RequestParam String F_QSRQ, @RequestParam String F_JZRQ, @RequestParam String F_HTBH, 
			                           @RequestParam String F_XMBH, @RequestParam String F_KHBH, EMPPageQuery page, @RequestParam String F_HTMC, 
			                           @RequestParam String F_XMMC, @RequestParam String F_KHBHMC, Model model) {
		JParamObject           po = JParamObject.Create();
		EFDataSet    queryDataSet = null;
		EFRowSet      queryRowSet = null;
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		
		po.SetValueByParamName("F_QSRQ", F_QSRQ);
		po.SetValueByParamName("F_JZRQ", F_JZRQ);
		po.SetValueByParamName("F_HTBH", F_HTBH);
		po.SetValueByParamName("F_XMBH", F_XMBH);
		po.SetValueByParamName("F_KHBH", F_KHBH);
		po.SetValueByParamName("F_CODE", user.getUSER_ORGID());
		po.setValue(EMPQueryParamEnum.PAGEQUERY, page);
		
		JResponseObject        RO = queryAnalyseServiceMgr.searchPSHTBMStatus(po);
		queryRowSet = (EFRowSet) RO.getResponseObject();
		
		queryDataSet = queryRowSet.getDataSet(EMPQueryParamEnum.QUERYRESULT);
		page = (EMPPageQuery) queryRowSet.getExtProperty(EMPQueryParamEnum.PAGEQUERY, null);
		model.addAttribute("queryDataSet", queryDataSet);
		model.addAttribute("page", page);
		model.addAttribute("F_CODE", user.getUSER_ORGID());
		model.addAttribute("F_QSRQ", F_QSRQ);
		model.addAttribute("F_JZRQ", F_JZRQ);
		model.addAttribute("F_KHBH", F_KHBH);
		model.addAttribute("F_HTBH", F_HTBH);
		model.addAttribute("F_XMBH", F_XMBH);
		model.addAttribute("F_KHMC", F_KHBHMC);
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_XMMC", F_XMMC);
		return "/cms/queryAnalyse/PSHT/pshtQueryList";
	}
	
	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/lcOpenPSHT/{F_GUID}")
	public String lcOpenPSHT(@PathVariable("F_GUID") String F_GUID, Model model) {
		JParamObject           PO = JParamObject.Create();
		EFFormDataModel formModel;
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + F_GUID + "'");
		
		try {
			formModel = formModelUtil.loadForm(PO);
			EFRowSet rowset = formModel.getBillDataSet().getRowSet(0);
			EFDataSet dataset = rowset.getDataSet("PSHTMX");

			rowset.putString("FLOW_NODE_NAME", "提交任务");
			model.addAttribute("canEdit", "0");
			model.addAttribute("canSubmit", "0");
			model.addAttribute("canRetrieve", "0");
			model.addAttribute("canRollback", "0");
//			
//			//判断是否超期
//			for(int i = 0; i < dataset.getRowCount(); i++) {
//				endDate = "";
//				itemRowset = dataset.getRowSet(i);
//				jzrq = itemRowset.getDate("F_JZRQ", null);
//				xgJzrq = itemRowset.getDate("XG_JZRQ", null);
//				qrrq = itemRowset.getDate("F_QRRQ", null);
//				
//				date = (xgJzrq == null ? jzrq : xgJzrq);
//				if(date != null) endDate = date.toString().replaceAll("-", "");
//				
//				if(qrrq == null) {
//					date = (xgJzrq == null ? jzrq : xgJzrq);
//					if(date != null) endDate = date.toString().replaceAll("-", "");
//					curDate = DateUtil.date3String("yyyyMMdd");
//					
//					if(!endDate.equals("")) {
//						if(curDate.compareTo(endDate) < 0) {
//							itemRowset.putString("F_QRZT", "1");
//						} else if(curDate.compareTo(endDate) > 0){
//							itemRowset.putString("F_QRZT", "2");
//						} else {
//							itemRowset.putString("F_QRZT", "0");
//						}
//					}	
//				} else {
//					curDate = qrrq.toString().replaceAll("-", "");
//					if(curDate.compareTo(endDate) <= 0) {
//						itemRowset.putString("F_QRZT", "1");
//					} else if(curDate.compareTo(endDate) > 0){
//						itemRowset.putString("F_QRZT", "2");
//					}
//				}			
//			}
//			
			model.addAttribute("curDate", DateUtil.date3String(DateUtil.PATTERN_DATE));
			model.addAttribute("dictObject", rowset);
			model.addAttribute("dataset", dataset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/queryAnalyse/PSHT/form/form";
	}	
}
